﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using HisoftSmart.Client.AutomationClient.UIAutomationServiceReference;
using HisoftSmart.Client.AutomationClient.Helpers;

namespace HisoftSmart.Client.AutomationClient.Actions
{
    public class SetCheckBoxAction : ActionBase
    {
        public bool IsChecked;

        public SetCheckBoxAction(string name, string _params)
            : base(name, _params)
        {
            IsChecked = bool.Parse(this.Params.Split(',')[1]);
            AutomationIdentifier = new AutomationIdentifier(this.Params.Split(',')[0], AutomationIdentification.TryElementName);
        }

        protected override void DoImpl()
        {
            var element = GetUIElement();
            if (element == null)
                return;

            if (AutomationElementFinder.SetElementProperty<bool>(element, "IsChecked", IsChecked))
            {
                SendSuccessResult();
                return;
            }

            if (AutomationElementFinder.SetElementProperty<bool>(element, "IsChecked", IsChecked))
            {
                SendSuccessResult();
                return;
            }

            if (AutomationElementFinder.SetElementProperty<bool>(element, "IsChecked", IsChecked))
            {
                SendSuccessResult();
                return;
            }

            // if text, password and content are all missing... then give up
            SendNotFoundResult();
        }
    }
}
